<template>
    <el-dialog :title="title" :visible.sync="visible" width="960px" append-to-body>
        <el-form ref="form" :model="formdata" :rules="rules" label-width="110px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="活动名称" prop="name">
                        <el-input placeholder="请输入活动名称" v-model="formdata.name"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="活动时间" prop="activityTime">
                        <el-date-picker
                            v-model="formdata.activityTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择活动时间">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="活动地点" prop="position">
                        <el-input placeholder="请输入活动地点" v-model="formdata.position"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="活动类型" prop="activityType">
                        <el-select v-model="formdata.activityType" placeholder="请选择活动类型" learable class="elSelect">
                            <el-option
                                v-for="dict in dict.type.school_activity_type"
                                :key="dict.value"
                                :label="dict.label"
                                :value="dict.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="活动附件" prop="attachmentPath">
                        <el-upload
                            class="upload-demo"
                            :action="uploadAction"
                            :headers="uploadHeaders"
                            :show-file-list="false"
                            :on-success="onSuccess"
                            :multiple="false"
                            >
                            <el-button
                                type="primary"
                                plain
                                icon="el-icon-upload2"
                                size="mini"
                            >上传附件</el-button>
                        </el-upload>
                        <span>{{formdata.attachmentPath}}</span>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="备注" prop="remark">
                        <el-input placeholder="请输入备注" v-model="formdata.remark"/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="visible = false">取 消</el-button>
        </div>
    </el-dialog>
    </template>
    
    <script>
    import { 
        addActivity,
        editActivity
    } from "@/api/leagueManagement/activity";
    import { getToken } from "@/utils/auth";
    export default {
        name: 'organizationAddDlg',
        data () {
            return {
                title: '添加',
                visible: false,
                formdata: {
                    name: '',
                    activityTime: '',
                    position: '',
                    activityType: '',
                    attachmentPath: '',
                    remark: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请输入活动名称', trigger: 'blur' }
                    ],
                    activityTime: [
                        { required: true, message: '请选择活动时间', trigger: 'blur' }
                    ],
                    position: [
                        { required: true, message: '请输入活动地点', trigger: 'blur' }
                    ],
                    activityType: [
                        { required: true, message: '请选择活动类型', trigger: 'change' }
                    ]
                },
                uploadAction: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
                uploadHeaders: {
                    Authorization: "Bearer " + getToken(),
                }
            }
        },
        dicts: [
            "school_activity_type", // 校园文化活动类型
        ],
        props: {
            mainType:{
                type: Number,
                default: 1
            }  
        },
        methods: {
            open (rowdata) {
                this.visible = true
                if (rowdata) {
                    this.formdata = rowdata
                } else {
                    this.formdata = {
                        name: '',
                        activityTime: '',
                        position: '',
                        activityType: '',
                        attachmentPath: '',
                        remark: ''
                    }
                }
            },
            submitForm () {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.formdata.id != undefined) {
                            editActivity({
                                ...this.formdata,
                                mainType: this.mainType
                            }).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.visible = false;
                                this.$emit('updateList');
                            });
                        } else {
                            addActivity({
                                ...this.formdata,
                                mainType: this.mainType
                            }).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.visible = false;
                                this.$emit('updateList');
                            });
                        }
                    }
                });
            },
            //文件上传成功
            onSuccess(response) {
                if (response.code == 200) {
                    this.formdata.attachmentPath = response.url
                } else {
                    this.$message.error(response.msg);
                }
            },
        }
    
    }
    </script>
    
    <style lang="scss" scoped>
    
    </style>